package com.ruiyian.netty.nettyserver;

import com.ruiyian.common.constant.Constants;
import com.ruiyian.common.thread.ThreadPoolUtil;
import com.ruiyian.netty.nettyTcp.NettyTcpServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/9/16
 * Desc: netty 服务端口开启
 */

@Slf4j
@Component
public class NettyServerConfig implements CommandLineRunner {
    @Value("${initStart.ifOpenSocketPort}")
    private Boolean ifOpenSocketPort;
    /**
     * 启动后自动开启服务
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        try {
            if(ifOpenSocketPort){
               /* ThreadPoolUtil.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            new NettyServer(Constants.MODULE_PORT).start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                ThreadPoolUtil.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            new NettyTcpServer().run(Constants.TCP_PORT);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });*/
                ThreadPoolUtil.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            new NettyServer(Constants.MAIN_PORT).start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

        } catch (Exception e) {
            log.error("Netty启动失败",e.getMessage());
        }
    }
}
